package cn.peach.org.controller;

import cn.peach.basic.util.AjaxResult;
import cn.peach.basic.util.PageList;
import cn.peach.org.domain.Employee;
import cn.peach.org.query.EmployeeQuery;
import cn.peach.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口", description = "员工接口详细描述")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据", notes = "")
    public Employee findById(@PathVariable("id") Long id) {
        return  employeeService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有",notes = "")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryList(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "传一个id")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("垃圾系统。。。");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "传数组id")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统崩坏");
        }
    }

    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "有id就是修改反之就是添加")
    public AjaxResult addAndUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null){
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("垃圾系统404");
        }
    }

}
